package com.htfyun.ft.fragment.factory;

import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.htfyun.ft.R;
import com.htfyun.ft.fragment.BaseFragment;
import com.htfyun.ft.fragment.MyHandler;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.ft.viewmodel.ViewModelFT;
import com.htfyun.ft.viewmodel.ViewModelFTGot;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public abstract class FactoryBaseFragment extends BaseFragment {

    private ViewModelFT viewModelFT;
    private final Object lock = new Object();

    private FactoryTestItemsEnum delayAutoPassEnum = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @NonNull
    protected ViewModelFT getViewModelFT() {
        if (viewModelFT == null) {
            synchronized (lock) {
                viewModelFT = ViewModelFTGot.getViewModel(getActivity());
            }
        }

        return viewModelFT;
    }

    @Override
    @CallSuper
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        View pass = findViewById(R.id.btnPass);
        View fail = findViewById(R.id.btnFail);

        if (pass != null) {
            pass.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finishMeWithPassed(true);
                }
            });
        }

        if (fail != null) {
            fail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finishMeWithPassed(false);
                }
            });
        }
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        delayAutoPassEnum = null;
    }

    protected void setBtnPassVisibility(int visibility) {
        View btn = findViewById(R.id.btnPass);
        if (btn != null) {
            btn.setVisibility(visibility);
        }
    }

    protected void setBtnPassEnabled(boolean enabled) {
        View btn = findViewById(R.id.btnPass);
        if (btn != null) {
            btn.setEnabled(enabled);
        }
    }

    protected void setBtnFailEnabled(boolean enabled) {
        View btn = findViewById(R.id.btnFail);
        if (btn != null) {
            btn.setEnabled(enabled);
        }
    }

    protected void finishMeWithPassed(boolean passed) {

        DebugLog.e("finishMeWithPassed this = " + this);

        FactoryTestItemsEnum itemsEnum = FactoryTestItemsEnum.getItemEnumByFragment(this);
        if (itemsEnum == null) {
            return;
        }

        ResultAttr result = ResultAttr.PASS;

        if (!passed) {
            result = ResultAttr.FAIL;
        }

        itemsEnum.setResult(result);

        getViewModelFT().setClickedItem(FactoryTestItemsEnum.main);

    }

    protected void delayAutoPass() {

        final FactoryTestItemsEnum itemsEnum = FactoryTestItemsEnum.getItemEnumByFragment(this);
        if (delayAutoPassEnum == itemsEnum) {
            return;
        }
        delayAutoPassEnum = itemsEnum;

        getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finishMeWithPassed(true);

                if (delayAutoPassEnum != null) {
                    String toast = delayAutoPassEnum.getName() + " " + getString(R.string.test_result_pass);
                    Toast.makeText(getContext(), toast, Toast.LENGTH_LONG).show();

                }
            }
        }, 1500);
    }


    public void dispatchTouchEvent(MotionEvent ev) {
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }
}
